/**
 * Copyright(c) kyle.
 */
package org.example.tomcat.bio.basic;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * 请求
 * 用字符串切割的方法提取请求方式和url
 *
 * @author kyle
 * @version 1.00.00
 * @since [2020-10-20 18:20]
 * @updateUser [Kyle]
 * @updateRemark [说明本次修改内容]
 * @date [2020-10-20 18:20]
 */
public class GPRequest {
    private String method;
    private String url;

    public GPRequest(InputStream in) {
        try {
            StringBuilder builder = new StringBuilder();
            byte[] buff = new byte[1024];
            int len = -2;

            while ((len = in.read(buff)) > 0) {
                builder.append(new String(buff, 0, len, StandardCharsets.UTF_8));
                if (len < buff.length) {
                    break;
                }
            }

            String content = builder.toString();
            System.out.println(content);
            if (!"".equalsIgnoreCase(content)) {
                String line = content.split("\\n")[0];
                String[] arr = line.split("\\s");

                method = arr[0];
                url = arr[1].split("\\?")[0];
            }
        } catch (IOException pE) {
            pE.printStackTrace();
        }
    }

    public String getUrl() {
        return url;
    }

    public String getMethod() {
        return method;
    }
}
